﻿namespace EasyDDD.AdminModule.Application.QueriesSysAdmin
{
    public record SysAdminGetRoleIdsQurery : IQuery<List<Guid>>
    {
        public Guid UserId { get; set; }
    }

    public class SysAdminGetRoleIdsQureryValidator : AbstractValidator<SysAdminGetRoleIdsQurery>
    {
        public SysAdminGetRoleIdsQureryValidator(Records.SysAdminRecords records)
        {
            var query = records.AsQueryable();

            RuleFor(x => x).Must(input => query.Any(e => e.Id == input.UserId))
                .WithMessage("找不到该记录");
        }
    }

    public class SysAdminGetRoleIdsQueryHandler(
       //IMapper mapper,
       Records.SysAdminRecords records,
       ILogger<SysAdminGetRoleIdsQueryHandler> logger)
       : IQueryHandler<SysAdminGetRoleIdsQurery, List<Guid>>
    {
        public async Task<List<Guid>> Handle(SysAdminGetRoleIdsQurery request, CancellationToken cancellationToken)
        {
            logger.LogDebug("查询 SysAdminGetRoleIdsQueryHandler");
            var query = records.AsQueryable();
            var entity = await query.Include(e => e.UserRoleList).FirstAsync(e => e.Id == request.UserId);
            var roleIds = entity.UserRoleList.Select(e => e.RoleId).ToList();
            return roleIds;
        }
    }
}
